package com.imay.live.view.custom;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.live.activity.authentication.AuthenticationActivity;
import com.imay.live.activity.profit.IncomeActivity;
import com.imay.live.activity.user.ResigterActivity;
import com.imay.live.R;

/**
 * Created by Administrator on 2017/04/28 0028.
 */

public class ReallyNameView extends LinearLayout {
    private Paint paint;
    private Path path;
    private int tag = 0;
    TextView texttip;
    ImageView imageView;
    public ReallyNameView(Activity activity, int tag) {
        super(activity);
        this.tag = tag;
        init(activity);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (path == null) {
            path = new Path();
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(4);
            paint.setColor(getResources().getColor(R.color.line_ec));
        }
        path.reset();
        path.moveTo(0, canvas.getHeight());
        path.lineTo(canvas.getWidth(), canvas.getHeight());
        canvas.drawPath(path, paint);
    }

    private void init(final Activity activity) {
        setOrientation(HORIZONTAL);
        setBackgroundColor(getResources().getColor(R.color.bg_window));
        TextView texttitle = new TextView(activity);
        texttitle.setTextColor(getResources().getColor(R.color.text_21));
        LayoutParams titleparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleparams.leftMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        titleparams.rightMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        titleparams.gravity = Gravity.CENTER_VERTICAL;
        addView(texttitle, titleparams);
        texttip = new TextView(activity);

        texttip.setTextColor(getResources().getColor(R.color.unread_point_bg));
        LayoutParams tipparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tipparams.weight = 1;
        tipparams.rightMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        texttip.setGravity(Gravity.RIGHT);
        tipparams.gravity = Gravity.CENTER_VERTICAL;
        addView(texttip, tipparams);
        imageView = new ImageView(activity);
        imageView.setBackgroundResource(R.mipmap.xiaojiantou);

        LayoutParams imageparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imageparams.rightMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        imageparams.gravity = Gravity.CENTER_VERTICAL;
        addView(imageView, imageparams);

        if (tag == 0) {
            texttitle.setText(getResources().getString(R.string.profit_reallyname));
            texttip.setText(getResources().getString(R.string.profitunmae));
        } else {
            texttitle.setText(getResources().getString(R.string.profitphone));
            texttip.setText(getResources().getString(R.string.profitunphone));
        }
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageView.getVisibility()==VISIBLE){
                    if (tag == 0) {
                        AuthenticationActivity.startForResult(activity, IncomeActivity.RECARDCODE);
                    } else {
                        ResigterActivity.startForResult(activity, IncomeActivity.RECODE, ResigterActivity.STATUS_BIND_PHONE);
                    }
                }

            }
        });
    }

    public void setText(CharSequence text) {
        texttip.setText(text);
        texttip.setTextColor(getResources().getColor(R.color.text_b2));
        texttip.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.hint_size));
    }
    public void setRightVisibility(int visibility) {
        imageView.setVisibility(visibility);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) getResources().getDimension(R.dimen.profitreally_height), MeasureSpec.EXACTLY));
    }
}
